package com.yboot.framework.plugin.generator.utils;

import com.yboot.framework.plugin.generator.property.CodeGenProperty;
import com.yboot.framework.plugin.generator.property.EnumFileOption;
import org.apache.commons.collections.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author .
 * @date 2022-11-10
 */
public class CodeGenUtil {

    private final static String TEMPLATE_CONTROLLER = "template/Controller.java.vm";
    private final static String TEMPLATE_SERVICE = "template/Service.java.vm";
    private final static String TEMPLATE_SERVICE_IMPL = "template/ServiceImpl.java.vm";
    private final static String TEMPLATE_MAPPER = "template/Dao.java.vm";
    private final static String TEMPLATE_MAPPER_XML = "template/Mapper.xml.vm";
    private final static String TEMPLATE_DTO = "template/Dto.java.vm";
    private final static String TEMPLATE_VO = "template/Vo.java.vm";
    private final static String TEMPLATE_DETAIL_VO = "template/DetailVo.java.vm";
    private final static String TEMPLATE_QUERY = "template/Query.java.vm";
    private final static String TEMPLATE_Entity = "template/Entity.java.vm";

    public static List<String> getTemplates() {
        List<String> templates = new ArrayList<>();
        templates.add(TEMPLATE_CONTROLLER);
        templates.add(TEMPLATE_SERVICE);
        templates.add(TEMPLATE_SERVICE_IMPL);
        templates.add(TEMPLATE_MAPPER);
        templates.add(TEMPLATE_MAPPER_XML);
        templates.add(TEMPLATE_DTO);
        templates.add(TEMPLATE_VO);
        templates.add(TEMPLATE_DETAIL_VO);
        templates.add(TEMPLATE_QUERY);
        templates.add(TEMPLATE_Entity);

        return templates;
    }

    public static String getSavePath(String template, CodeGenProperty property, String clazzName) {

        String savePath;

        String javaSrcFolder = property.getOutput().getJavaSrcFolder();
        String basePackage = property.getOutput().getBasePackage();
        String basePackageFolder = basePackage.replace(".", File.separator) + File.separator;

        switch (template) {
            case TEMPLATE_CONTROLLER:
                String controllerPackageName = property.getOutput().getControllerPackageName();
                savePath = javaSrcFolder + basePackageFolder + controllerPackageName + File.separator + clazzName + "Controller.java";
                break;
            case TEMPLATE_SERVICE:
                String servicePackageName = property.getOutput().getServicePackageName();
                savePath = javaSrcFolder + basePackageFolder + servicePackageName + File.separator + "I" + clazzName + "Service.java";
                break;
            case TEMPLATE_SERVICE_IMPL:
                String serviceImplPackageName = property.getOutput().getServiceImplPackageName();
                savePath = javaSrcFolder + basePackageFolder + serviceImplPackageName + File.separator + clazzName + "ServiceImpl.java";
                break;
            case TEMPLATE_MAPPER:
                String mapperPackageName = property.getOutput().getMapperPackageName();;
                savePath = javaSrcFolder + basePackageFolder + mapperPackageName + File.separator + clazzName + "Mapper.java";
                break;
            case TEMPLATE_MAPPER_XML:
                String javaResourcesFolder = property.getOutput().getJavaResourcesFolder();
                String mapperXmlFolder = property.getOutput().getMapperXmlFolder();
                savePath = javaResourcesFolder + mapperXmlFolder + File.separator + clazzName + "Mapper.xml";
                break;
            case TEMPLATE_DTO:
                String dtoPackageName = property.getOutput().getDtoPackageName();
                savePath = javaSrcFolder + basePackageFolder + dtoPackageName + File.separator + clazzName + "Dto.java";
                break;
            case TEMPLATE_VO:
                String poPackageName = property.getOutput().getVoPackageName();
                savePath = javaSrcFolder + basePackageFolder + poPackageName + File.separator + clazzName + "Vo.java";
                break;
            case TEMPLATE_DETAIL_VO:
                String detailPoPackageName = property.getOutput().getVoPackageName();
                savePath = javaSrcFolder + basePackageFolder + detailPoPackageName + File.separator + clazzName + "DetailVo.java";
                break;
            case TEMPLATE_QUERY:
                String queryPackageName = property.getOutput().getQueryPackageName();
                savePath = javaSrcFolder + basePackageFolder + queryPackageName + File.separator + clazzName + "Query.java";
                break;
            case TEMPLATE_Entity:
                String entityPackageName = property.getOutput().getEntityPackageName();
                savePath = javaSrcFolder + basePackageFolder + entityPackageName + File.separator + File.separator + clazzName + "Entity.java";
                break;
            default:
                savePath = null;
        }

        return savePath;
    }

    public static boolean skip(String template, CodeGenProperty property) {

        List<EnumFileOption> options = property.getGenOption().getExcludeFileOptions();

        if (CollectionUtils.isEmpty(options)) {
            return false;
        }

        boolean skip;

        switch (template) {
            case TEMPLATE_CONTROLLER:
                skip = options.contains(EnumFileOption.Controller);
                break;
            case TEMPLATE_SERVICE:
                skip = options.contains(EnumFileOption.Service);
                break;
            case TEMPLATE_SERVICE_IMPL:
                skip = options.contains(EnumFileOption.ServiceImpl);
                break;
            case TEMPLATE_MAPPER:
                skip = options.contains(EnumFileOption.Mapper);
                break;
            case TEMPLATE_MAPPER_XML:
                skip = options.contains(EnumFileOption.MapperXml);
                break;
            case TEMPLATE_DTO:
                skip = options.contains(EnumFileOption.Dto);
                break;
            case TEMPLATE_VO:
                skip = options.contains(EnumFileOption.Po);
                break;
            case TEMPLATE_QUERY:
                skip = options.contains(EnumFileOption.Query);
                break;
            case TEMPLATE_Entity:
                skip = options.contains(EnumFileOption.Vo);
                break;
            default:
                skip = false;
        }

        return skip;
    }

}
